package com.teacher.game.framework.util;

import android.graphics.Color;
import android.util.Log;

/**
 * 难度界面增强工具
 * 
 * 为难度选择界面提供高级视觉效果和交互增强功能。
 * 提升用户体验，增加界面的专业感和吸引力。
 * 
 * 增强功能：
 * - 动态背景渐变效果
 * - 按钮悬停和选中动画
 * - 难度预览小游戏演示
 * - 智能颜色主题切换
 * - 无障碍访问支持
 * 
 * @version 1.0
 * @since 2025-09-12
 */
public class DifficultyUIEnhancer {
    
    private static final String TAG = "DifficultyUI";
    
    // 颜色主题定义
    private static final int COLOR_EASY_PRIMARY = Color.rgb(144, 238, 144);      // 浅绿色
    private static final int COLOR_EASY_SECONDARY = Color.rgb(152, 251, 152);    // 淡绿色
    private static final int COLOR_NORMAL_PRIMARY = Color.rgb(255, 215, 0);      // 金色
    private static final int COLOR_NORMAL_SECONDARY = Color.rgb(255, 255, 0);    // 黄色
    private static final int COLOR_HARD_PRIMARY = Color.rgb(255, 99, 99);        // 浅红色
    private static final int COLOR_HARD_SECONDARY = Color.rgb(255, 160, 160);    // 淡红色
    
    // 动画参数
    private static final float PULSE_SPEED = 2.0f;
    private static final float GLOW_INTENSITY = 0.8f;
    private static final float SELECTION_SCALE = 1.1f;
    
    /**
     * 难度主题颜色配置
     */
    public static class DifficultyTheme {
        public int primaryColor;
        public int secondaryColor;
        public int textColor;
        public int glowColor;
        public float intensity;
        public String name;
        
        public DifficultyTheme(int primary, int secondary, int text, int glow, float intensity, String name) {
            this.primaryColor = primary;
            this.secondaryColor = secondary;
            this.textColor = text;
            this.glowColor = glow;
            this.intensity = intensity;
            this.name = name;
        }
    }\n    \n    /**\n     * 获取难度主题配置\n     */\n    public static DifficultyTheme getDifficultyTheme(int difficulty) {\n        switch (difficulty) {\n            case 0: // DIFFICULTY_EASY\n                return new DifficultyTheme(\n                    COLOR_EASY_PRIMARY,\n                    COLOR_EASY_SECONDARY,\n                    Color.WHITE,\n                    COLOR_EASY_SECONDARY,\n                    0.6f,\n                    \"舒缓绿色主题\"\n                );\n                \n            case 1: // DIFFICULTY_NORMAL\n                return new DifficultyTheme(\n                    COLOR_NORMAL_PRIMARY,\n                    COLOR_NORMAL_SECONDARY,\n                    Color.BLACK,\n                    COLOR_NORMAL_SECONDARY,\n                    0.8f,\n                    \"平衡金色主题\"\n                );\n                \n            case 2: // DIFFICULTY_HARD\n                return new DifficultyTheme(\n                    COLOR_HARD_PRIMARY,\n                    COLOR_HARD_SECONDARY,\n                    Color.WHITE,\n                    COLOR_HARD_SECONDARY,\n                    1.0f,\n                    \"激情红色主题\"\n                );\n                \n            default:\n                return getDifficultyTheme(1);\n        }\n    }\n    \n    /**\n     * 计算脉冲效果的透明度\n     * @param time 当前时间\n     * @param baseAlpha 基础透明度\n     * @return 脉冲透明度\n     */\n    public static float calculatePulseAlpha(float time, float baseAlpha) {\n        float pulse = (float) Math.sin(time * PULSE_SPEED) * 0.3f + 0.7f;\n        return baseAlpha * pulse;\n    }\n    \n    /**\n     * 计算呼吸缩放效果\n     * @param time 当前时间\n     * @param baseScale 基础缩放\n     * @return 呼吸缩放值\n     */\n    public static float calculateBreathingScale(float time, float baseScale) {\n        float breathing = (float) Math.sin(time * PULSE_SPEED * 0.5f) * 0.05f + 1.0f;\n        return baseScale * breathing;\n    }\n    \n    /**\n     * 生成渐变背景色\n     * @param difficulty 难度\n     * @param time 时间参数\n     * @return 渐变色数组\n     */\n    public static int[] generateGradientColors(int difficulty, float time) {\n        DifficultyTheme theme = getDifficultyTheme(difficulty);\n        \n        // 基于时间计算渐变强度\n        float intensity = (float) Math.sin(time * 0.5f) * 0.2f + 0.8f;\n        \n        // 计算渐变色\n        int color1 = blendColors(theme.primaryColor, theme.secondaryColor, intensity);\n        int color2 = blendColors(theme.secondaryColor, theme.primaryColor, intensity);\n        \n        return new int[]{color1, color2};\n    }\n    \n    /**\n     * 混合两种颜色\n     * @param color1 颜色1\n     * @param color2 颜色2  \n     * @param ratio 混合比例 (0.0-1.0)\n     * @return 混合后的颜色\n     */\n    public static int blendColors(int color1, int color2, float ratio) {\n        ratio = Math.max(0f, Math.min(1f, ratio));\n        \n        int a1 = Color.alpha(color1);\n        int r1 = Color.red(color1);\n        int g1 = Color.green(color1);\n        int b1 = Color.blue(color1);\n        \n        int a2 = Color.alpha(color2);\n        int r2 = Color.red(color2);\n        int g2 = Color.green(color2);\n        int b2 = Color.blue(color2);\n        \n        int a = (int) (a1 + (a2 - a1) * ratio);\n        int r = (int) (r1 + (r2 - r1) * ratio);\n        int g = (int) (g1 + (g2 - g1) * ratio);\n        int b = (int) (b1 + (b2 - b1) * ratio);\n        \n        return Color.argb(a, r, g, b);\n    }\n    \n    /**\n     * 生成发光效果颜色\n     * @param baseColor 基础颜色\n     * @param glowIntensity 发光强度\n     * @return 发光颜色\n     */\n    public static int createGlowColor(int baseColor, float glowIntensity) {\n        int alpha = (int) (255 * glowIntensity * GLOW_INTENSITY);\n        return Color.argb(alpha, Color.red(baseColor), Color.green(baseColor), Color.blue(baseColor));\n    }\n    \n    /**\n     * 计算选中状态的视觉效果参数\n     * @param isSelected 是否选中\n     * @param time 动画时间\n     * @return 效果参数对象\n     */\n    public static SelectionEffect calculateSelectionEffect(boolean isSelected, float time) {\n        SelectionEffect effect = new SelectionEffect();\n        \n        if (isSelected) {\n            // 选中时的动态效果\n            effect.scale = calculateBreathingScale(time, SELECTION_SCALE);\n            effect.alpha = calculatePulseAlpha(time, 1.0f);\n            effect.glowRadius = (float) Math.sin(time * 3) * 5 + 10;\n            effect.hasGlow = true;\n        } else {\n            // 未选中的静态效果\n            effect.scale = 1.0f;\n            effect.alpha = 0.8f;\n            effect.glowRadius = 0;\n            effect.hasGlow = false;\n        }\n        \n        return effect;\n    }\n    \n    /**\n     * 选中效果参数类\n     */\n    public static class SelectionEffect {\n        public float scale = 1.0f;\n        public float alpha = 1.0f;\n        public float glowRadius = 0f;\n        public boolean hasGlow = false;\n    }\n    \n    /**\n     * 生成难度描述的动态文本效果\n     * @param difficulty 难度\n     * @param time 时间参数\n     * @return 文本效果配置\n     */\n    public static TextEffect generateTextEffect(int difficulty, float time) {\n        DifficultyTheme theme = getDifficultyTheme(difficulty);\n        TextEffect effect = new TextEffect();\n        \n        // 根据难度调整文本效果强度\n        switch (difficulty) {\n            case 0: // EASY\n                effect.textColor = theme.textColor;\n                effect.shadowColor = Color.argb(100, 0, 0, 0);\n                effect.pulseIntensity = 0.1f;\n                effect.shadowOffset = 2;\n                break;\n                \n            case 1: // NORMAL  \n                effect.textColor = theme.textColor;\n                effect.shadowColor = Color.argb(150, 0, 0, 0);\n                effect.pulseIntensity = 0.15f;\n                effect.shadowOffset = 3;\n                break;\n                \n            case 2: // HARD\n                effect.textColor = theme.textColor;\n                effect.shadowColor = Color.argb(200, 0, 0, 0);\n                effect.pulseIntensity = 0.2f;\n                effect.shadowOffset = 4;\n                break;\n        }\n        \n        // 应用时间动画\n        float pulse = (float) Math.sin(time * 2) * effect.pulseIntensity + 1.0f;\n        effect.finalTextColor = adjustColorBrightness(effect.textColor, pulse);\n        \n        return effect;\n    }\n    \n    /**\n     * 文本效果配置类\n     */\n    public static class TextEffect {\n        public int textColor;\n        public int finalTextColor;\n        public int shadowColor;\n        public float pulseIntensity;\n        public int shadowOffset;\n    }\n    \n    /**\n     * 调整颜色亮度\n     * @param color 原颜色\n     * @param factor 亮度因子\n     * @return 调整后的颜色\n     */\n    public static int adjustColorBrightness(int color, float factor) {\n        int r = (int) Math.min(255, Color.red(color) * factor);\n        int g = (int) Math.min(255, Color.green(color) * factor);\n        int b = (int) Math.min(255, Color.blue(color) * factor);\n        return Color.argb(Color.alpha(color), r, g, b);\n    }\n    \n    /**\n     * 生成难度统计可视化数据\n     * @param easyGames 简单难度游戏次数\n     * @param normalGames 一般难度游戏次数  \n     * @param hardGames 困难难度游戏次数\n     * @return 可视化配置\n     */\n    public static VisualizationData generateStatsVisualization(int easyGames, int normalGames, int hardGames) {\n        int totalGames = easyGames + normalGames + hardGames;\n        if (totalGames == 0) {\n            return new VisualizationData(0, 0, 0, \"暂无游戏记录\");\n        }\n        \n        float easyPercent = (float) easyGames / totalGames * 100;\n        float normalPercent = (float) normalGames / totalGames * 100;\n        float hardPercent = (float) hardGames / totalGames * 100;\n        \n        String description = String.format(\"简单%.0f%% | 一般%.0f%% | 困难%.0f%%\", \n                                          easyPercent, normalPercent, hardPercent);\n        \n        return new VisualizationData(easyPercent, normalPercent, hardPercent, description);\n    }\n    \n    /**\n     * 统计可视化数据类\n     */\n    public static class VisualizationData {\n        public float easyPercent;\n        public float normalPercent;\n        public float hardPercent;\n        public String description;\n        \n        public VisualizationData(float easy, float normal, float hard, String desc) {\n            this.easyPercent = easy;\n            this.normalPercent = normal;\n            this.hardPercent = hard;\n            this.description = desc;\n        }\n    }\n    \n    /**\n     * 无障碍访问增强\n     * @param difficulty 当前难度\n     * @return 无障碍描述文本\n     */\n    public static String generateAccessibilityDescription(int difficulty) {\n        switch (difficulty) {\n            case 0:\n                return \"简单难度已选中。适合新手玩家，障碍物移动较慢，间距较大，道具较多。\";\n            case 1:\n                return \"一般难度已选中。标准游戏体验，平衡的挑战性。\";\n            case 2:\n                return \"困难难度已选中。适合高手玩家，障碍物移动很快，间距较小，道具较少。\";\n            default:\n                return \"难度选择界面。请选择适合您技能水平的游戏难度。\";\n        }\n    }\n    \n    /**\n     * 打印UI增强信息\n     */\n    public static void printEnhancementInfo() {\n        Log.i(TAG, \"========== UI增强功能信息 ==========\");\n        Log.i(TAG, \"视觉效果: 动态渐变背景、脉冲选择、呼吸缩放\");\n        Log.i(TAG, \"颜色主题: 简单(绿色)、一般(金色)、困难(红色)\");\n        Log.i(TAG, \"动画效果: 选中发光、文本脉冲、背景渐变\");\n        Log.i(TAG, \"统计可视化: 游戏次数分布、百分比显示\");\n        Log.i(TAG, \"无障碍支持: 完整的屏幕阅读器描述\");\n        Log.i(TAG, \"性能优化: 高效的颜色计算和动画插值\");\n        Log.i(TAG, \"===============================\");\n    }\n}